//
//  GGZFHViewController.m
//  Estate
//
//  Created by heyong on 16/6/13.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "GGZFHViewController.h"
#import "B_SwitchCell.h"
#import "QianDTableCell.h"
#import "B_wuLiaoCell.h"
#import "DefaineCell.h"
@interface GGZFHViewController ()
@end

@implementation GGZFHViewController

-(NSMutableArray *)selectedAssetURLs{
    if(_selectedAssetURLs==nil){
        _selectedAssetURLs=[NSMutableArray array];
    }
    return  _selectedAssetURLs;
}

- (NSMutableDictionary *)dataDic{
    if(_dataDic==nil){
        _dataDic=[NSMutableDictionary dictionary];
    }
    return _dataDic;
}
- (Site_05 *)site{
    if(_site==nil){
        _site=[[Site_05 alloc]init];
    }
    return _site;
}
-(TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        [LSUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
        [_tableView registerClass:[H1_ImagesCell class] forCellReuseIdentifier:kCellIdentifier_H1_ImagesCell];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"wuliao"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_SwitchCell" bundle:nil] forCellReuseIdentifier:@"switch"];
        [_tableView registerNib:[UINib nibWithNibName:@"QianDTableCell" bundle:nil] forCellReuseIdentifier:@"qd"];
        [_tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"de"];
    }
    return _tableView;
}
- (UIImagePickerController *)picker{
    if(!_picker){
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;
        _picker.allowsEditing = NO;//设置可编辑
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    return _picker;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview: self.tableView];
    self.view.backgroundColor=[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    _curTweet = [[Tweet alloc] init];
    [self setData];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(refresh:) name:@"RefreshImage_5" object:nil];
}

- (void)refresh:(id)sender{
    if(self.selectedAssetURLs!=nil){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //设置图片
            self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
            //回到主线程来刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:4]] withRowAnimation:UITableViewRowAnimationFade];
            });
        });
    }
}

- (void)setData{
    WEAKSELF
    if(_site!=nil){
        [weakSelf.dataDic setObject:_site.is_fh_1 forKey:gzz_fh_is1];
        [weakSelf.dataDic setObject:_site.is_fh_2 forKey:gzz_fh_is2];
        [weakSelf.dataDic setObject:_site.is_fh_3 forKey:gzz_fh_is3];
        [weakSelf.dataDic setObject:_site.is_fh_4 forKey:gzz_fh_is4];
        [weakSelf.dataDic setObject:_site.fh_1 forKey:gzz_fh_1];
        [weakSelf.dataDic setObject:_site.fh_2 forKey:gzz_fh_2];
        [weakSelf.dataDic setObject:_site.text forKey:fs_msg_10];
        self.selectedAssetURLs= [LSUtility quertFileImageType:@"RECORD_02_SIDE_04" andId:_site.recordId andFilePathArr:_site.fileArray];
        [self refresh:nil];
        
    }else{
        //默认为nil
        [weakSelf.dataDic setObject:@"是" forKey:gzz_fh_is1];
        [weakSelf.dataDic setObject:@"是" forKey:gzz_fh_is2];
        [weakSelf.dataDic setObject:@"是" forKey:gzz_fh_is3];
        [weakSelf.dataDic setObject:@"是" forKey:gzz_fh_is4];
        [weakSelf.dataDic setObject:@"" forKey:gzz_fh_1];
        [weakSelf.dataDic setObject:@"" forKey:gzz_fh_2];
        [weakSelf.dataDic setObject:@{@"内容":@""} forKey:fs_msg_10];
    }
    
}



#pragma mark-UITaleiViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 4;
    }else if (section==1){
        return 1;
    }else if (section==2){
        return 2;
    }else{
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    long section=indexPath.section;
    if (section==3){
        return 100;
    }else if(section==4){
          return  [H1_ImagesCell cellHeightWithObj:_curTweet title:@"附件"];
    }else{
        return 44;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }else
        return 10.;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    long section=indexPath.section;
    long row=indexPath.row;
    if(section==0){
        if(row==0){
            B_SwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"switch" forIndexPath:indexPath];
            cell.title.font=[UIFont systemFontOfSize:14*BIZ];
            cell.title.text=@"是否采用泥浆护壁";
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.is_fh_1=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:_site.is_fh_1 forKey:gzz_fh_is1];
            };
            
            if(_site){
                if([_site.is_fh_1 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
            
            return cell;
        }else if (row==1){
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qd" forIndexPath:indexPath];
            cell.title.font=[UIFont systemFontOfSize:14*BIZ];
            cell.textLabel.font=[UIFont systemFontOfSize:14*BIZ];
            cell.isedit=self.isEdit;
            cell.textValueChangedBlock=^(NSString *valueStr){
                [ weakSelf.dataDic setObject:valueStr forKey:gzz_fh_1];
            };
            

            [cell configWithPlaceholder:@"请输入" andValue:_site.fh_1 andLabel:@"泥浆护壁造浆方式" andKeyType:UIKeyboardTypeDefault];
            return cell;
        }else if (row==2){
            QianDTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qd" forIndexPath:indexPath];
            cell.title.font=[UIFont systemFontOfSize:14*BIZ];
            cell.textLabel.font=[UIFont systemFontOfSize:14*BIZ];
            
            cell.isedit=self.isEdit;
            cell.textValueChangedBlock=^(NSString *valueStr){
                [ weakSelf.dataDic setObject:valueStr forKey:gzz_fh_2];
            };
            [cell configWithPlaceholder:@"请输入" andValue:_site.fh_2 andLabel:@"泥浆比重抽测" andKeyType:UIKeyboardTypeDefault];
            return cell;
            
        }else{
            B_SwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"switch" forIndexPath:indexPath];
            cell.title.font=[UIFont systemFontOfSize:14*BIZ];
            cell.title.text=@"是否符合要求";
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.is_fh_2=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:_site.is_fh_2 forKey:gzz_fh_is2];
            };
            
            if(_site){
                if([_site.is_fh_2 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
            
            return cell;
        }
       
    }else if(section==1){
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"de" forIndexPath:indexPath];
        cell.title.text=@"终孔前及浇筑检查情况";
        cell.title.font=[UIFont systemFontOfSize:14*BIZ];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        return cell;
        
    }else if (section==2){
        B_SwitchCell *cell=[tableView dequeueReusableCellWithIdentifier:@"switch" forIndexPath:indexPath];
        cell.title.font=[UIFont systemFontOfSize:14*BIZ];
        if(row==0){
            cell.title.text=@"是否符合冬/雨季要求";
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.is_fh_3=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:_site.is_fh_3 forKey:gzz_fh_is3];
            };
            
            if(_site){
                if([_site.is_fh_3 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
            
            return cell;
        }else{
            cell.title.text=@"是否按照冬/雨季施工方案组织施工";
            cell.isEdit=self.isEdit;
            cell.sValueChangeBlock=^(UISwitch *sender){
                weakSelf.site.is_fh_4=sender.on==0?@"否":@"是";
                [weakSelf.dataDic setObject:_site.is_fh_4 forKey:gzz_fh_is4];
            };
            
            if(_site){
                if([_site.is_fh_4 isEqualToString:@"是"]){
                    //设置开关
                    [cell.kaiguan setOn:YES];
                }else{
                    [cell.kaiguan setOn:NO];
                }
            }
            
            
            return cell;
        }
    
    }else if(section==4){
        WEAKSELF
        H1_ImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_H1_ImagesCell forIndexPath:indexPath];
        cell.isEdit=self.isEdit;
        [cell setTitle:@"附件"];
        cell.curTweet = _curTweet;
        cell.addPicturesBlock = ^(){
            [weakSelf showActionForPhoto];
        };
        __block H1_ImagesCell *cellblock = cell;
        cell.deleteTweetImageBlockUrl = ^(TweetImage *toDelete,NSString *deurl){
            [cellblock.curTweet.tweetImages removeObject:toDelete];
            [weakSelf.selectedAssetURLs removeObject:deurl];
            [weakSelf refresh:nil];
        };
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        return cell;

    
    }
    else {
        B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"wuliao" forIndexPath:indexPath];
        cell.title.font=[UIFont systemFontOfSize:14.*BIZ];
        cell.text.font=[UIFont systemFontOfSize:14.*BIZ];
        cell.title.text=@"发现的问题及处理情况";
        cell.isEdit=self.isEdit;
        cell.textValueChangedBlock=^(NSString *valuerStr){
            weakSelf.site.text=@{@"内容":valuerStr};
            [weakSelf.dataDic setObject:weakSelf.site.text forKey:fs_msg_10];
        };
        if(_site){
            cell.text.text=_site.text[@"内容"];
        }
        
        return  cell;
    }
}
///!!!!:点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    if(indexPath.section==1){
        GzzFhSubController *subCtr=[[GzzFhSubController alloc]init];
        //查看传入模型
        subCtr.site=_site;
        subCtr.isEdit=self.isEdit;
        //二级界面值回调 存入 self.datDic
        subCtr.dataBlock=^(NSDictionary *dic){
            if(_site==nil){
                //新增回填土 保存。再次进入，值不会情况
                _site=[[Site_05 alloc]init];
            }
            //得到所有key 根据判断是否显示
            for (NSString *key in [dic allKeys]) {
                [dic setValue:@"是" forKey:[NSString stringWithFormat:@"%@是否显示",key]];
            }
            _site.fh_hnt=dic;
            [weakSelf.dataDic setObject:dic forKey:@"终孔前及浇筑检查情况"];
        };
        [self.navigationController pushViewController:subCtr animated:YES];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark UIActionSheet M

- (void)showActionForPhoto{
    //    @weakify(self);
    [[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
        
        [self photoActionSheet:sheet DismissWithButtonIndex:index];
        
    }] showInView:self.view];
}

- (void)photoActionSheet:(UIActionSheet *)sheet DismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //拍照
        if (_curTweet.tweetImages.count >= 9) {//照片最多这么多
            kTipAlert(@"最多只可选择9张照片，已经选满了。先去掉一张照片再拍照呗～");
            return;
        }
       
        [self presentViewController:self.picker animated:YES completion:nil];//进入照相界面
    }else if (buttonIndex == 1){
        //相册
        PictureViewController *picCtr=[[PictureViewController alloc]init];
        picCtr.flag=@"file";
        NSMutableArray *array = [self.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
        picCtr.selectedImages=[[NSMutableArray alloc] initWithArray:array];
        picCtr.selectedImageBlock=^(NSMutableArray *array){
            for (NSString *path in array) {
                //添加图片路径到已选数组
                [self.selectedAssetURLs addObject:path];
            }
        };
        [self.navigationController pushViewController:picCtr animated:YES];
    }
}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    self.curTweet.editImageView = [[EditImageView alloc]initWithImage:image];
    WEAKSELF
    self.curTweet.editImageView.completeBlock = ^(NSString *path) {
        //添加选择附件
        [weakSelf.curTweet addASelectedAssetURL:path];
        [weakSelf.selectedAssetURLs addObject:path];
        //刷新指定行
        [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:4]] withRowAnimation:UITableViewRowAnimationFade];
    };
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
